package com.zf.flutter_app;

import android.content.Context;
import android.util.Log;

import com.baidu.mapapi.map.MapView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import io.flutter.plugin.common.MessageCodec;
import io.flutter.plugin.platform.PlatformView;
import io.flutter.plugin.platform.PlatformViewFactory;

public class MapViewFactory extends PlatformViewFactory {

    private MainActivity mainActivity;

    public MapViewFactory(MessageCodec<Object> messageCodec,MainActivity mainActivity) {
        super(messageCodec);
        this.mainActivity = mainActivity;
    }

    @Override
    public PlatformView create(Context context, int position, Object o) {
        Map<String,Object> param = (Map<String,Object>)o;
        String data = (String)param.get("coordinates");
        List<MapData> datas = new ArrayList<>();
        try {
            JSONArray jsonArray = new JSONArray(data);
            Log.d("zhangfeng",jsonArray.toString());
            for(int j = 0;j<jsonArray.length();j++){
                JSONObject jsonObject = jsonArray.getJSONObject(j);
                MapData mapData = new MapData();
                mapData.lat = jsonObject.getString("lat");
                mapData.lng = jsonObject.getString("lng");
                mapData.name = jsonObject.getString("name");
                datas.add(mapData);
            }
        }catch (JSONException e){
            e.printStackTrace();
        }
        return new BDMapView(context,mainActivity,datas);
    }

}
